package com.dianrui.firemenmonitor.activity;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.TextView;

import com.androidkun.xtablayout.XTabLayout;
import com.dianrui.firemenmonitor.Constant;
import com.dianrui.firemenmonitor.R;
import com.dianrui.firemenmonitor.adapter.StudyPageAdapter;
import com.dianrui.firemenmonitor.bean.StudyTopBean;
import com.dianrui.firemenmonitor.event.StudyFragmentEvent;
import com.dianrui.firemenmonitor.fragment.FragStudyList;
import com.dianrui.firemenmonitor.util.OkHttpUtils;
import com.dianrui.firemenmonitor.util.StringUtils;
import com.dianrui.firemenmonitor.util.Url;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class StudyActivity extends BaseActivity {
    private List<StudyTopBean> studyTopBeanList=new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();

    @BindView(R.id.xTablayout)
    XTabLayout xTabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    @BindView(R.id.title)
    TextView title;


    @Override
    public int getContentViewId() {
        return R.layout.activity_study;
    }

    @Override
    public void initView() {
        customInit(R.color.blue_518bfa);
        title.setText(getString(R.string.study_title));
        getClassication();
        xTabLayout.addOnTabSelectedListener(new XTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(XTabLayout.Tab tab) {
                EventBus.getDefault().post(new StudyFragmentEvent(tab.getPosition()+1));

            }

            @Override
            public void onTabUnselected(XTabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(XTabLayout.Tab tab) {

            }
        });

    }

    public void getClassication(){
        OkHttpUtils.getInstance().postRequests(Url.CLASSIFCATIONS, "", new OkHttpUtils.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("code"))) {
                                if (studyTopBeanList != null) {
                                    studyTopBeanList.clear();
                                }
                                JSONArray jsonArray = jsonObject.optJSONObject("data").optJSONArray("column");
                                if (jsonArray.length()>0) {
                                    List<String> titles = new ArrayList<>();
                                    for (int i = 0; i <jsonArray.length() ; i++) {
                                        StudyTopBean studyTopBean=new StudyTopBean();
                                        JSONObject jsonObject1 = jsonArray.optJSONObject(i);
                                        studyTopBean.id= jsonObject1.optString("id");
                                        studyTopBean.name= jsonObject1.optString("name");
                                        studyTopBeanList.add(studyTopBean);
                                        titles.add(studyTopBeanList.get(i).name);
                                    }

                                    for (int i = 0; i < titles.size(); i++) {
                                        fragments.add(new FragStudyList());
                                    }
                                    StudyPageAdapter adatper = new StudyPageAdapter(getSupportFragmentManager(), fragments, titles);
                                    viewPager.setAdapter(adatper);
                                    viewPager.setOffscreenPageLimit(4);
                                    //将TabLayout和ViewPager关联起来。
                                    xTabLayout.setupWithViewPager(viewPager);
                                    //给TabLayout设置适配器
                                    xTabLayout.setupWithViewPager(viewPager);
                                    viewPager.setCurrentItem(0);
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }



    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
